import router from './router'
import store from './store'
import getPageTitle from '@/utils/get-page-title'

import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css'
import {getToken} from "@/utils/token"; // progress bar style

NProgress.configure({showSpinner: false}) // NProgress Configuration

//在每次一次跳转执行
router.beforeEach(async (to, from, next) => {
  // start progress bar
  NProgress.start()
  // set page title
  document.title = getPageTitle(to.meta.title)
  // determine whether the user has logged in
  const hasToken = getToken();

  if (hasToken) {
    if (to.path === '/login') {
      // 登录，跳转首页
      next({path: '/'})
      NProgress.done()
    } else {
      // 其他请求 获取用户信息
      await store.dispatch('user/getInfo')
      next()
    }
  } else {
    //未登录
    next()
  }
})

//跳转完成后关闭进度条
router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})